
package org.come.until;

import com.tool.image.ImageMixDeal;
import com.tool.tcp.GetTcpPath;
import com.tool.tcp.Sprite;
import com.tool.tcp.SpriteFactory;
import com.tool.tcpimg.RichLabel;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.JScrollPane;
import org.come.Frame.FriendChatMessageJframe;
import org.come.Frame.TestfriendlistJframe;
import org.come.bean.ChatBean;
import org.come.entity.Friendtable;
import org.come.socket.Agreement;
import org.come.socket.SendMessageUntil;

public class MessagrFlagUntil {
	public static List<String> NotReads = new ArrayList();
	public static Map<String, RichLabel> chatMap = new HashMap();
	public static String ImgFlagImg = "鼠标";
	public static String MOUSE1 = "鼠标";
	public static String MOUSE2 = "组队";
	public static String MOUSE3 = "给予";
	public static String MOUSE4 = "好友";
	public static String MOUSE5 = "交易";
	public static String MOUSE6 = "切磋";
	public static String MOUSE7 = "捕捉";
	public static String MOUSE8 = "施法状态";
	public static String MOUSE9 = "药品使用";
	public static String MOUSE10 = "加";//"加锁"
	public static String MOUSE11 = "解";//"解锁"
	public static String MOUSE12 = "手指";
	public static String MOUSE13 = "NCP";
	public static String MOUSE15 = "时间锁";
	private static Sprite mouse;

	public MessagrFlagUntil() {
	}

	public static void setMouse(String mouseImg) {
		ImgFlagImg = mouseImg;
		mouse = null;
	}

	public static Sprite getMouse() {
		if (mouse == null) {
			mouse = SpriteFactory.Prepare(GetTcpPath.getMouseTcp(ImgFlagImg));
		}

		return mouse;
	}

	public static RichLabel getRichLabel(String rolename) {
		RichLabel richLabel = (RichLabel)chatMap.get(rolename);
		if (richLabel == null) {
			richLabel = new RichLabel();
			richLabel.setSize(280, 16);
			chatMap.put(rolename, richLabel);
		}

		return richLabel;
	}

	public static void ReceiveMessage(ChatBean bean, String rolename) {
		StringBuffer buffer = new StringBuffer();
		buffer.append("#r#R ");
		buffer.append(bean.getRolename());
		buffer.append(" ");
		buffer.append(timeStamp2Date(bean.getTime(), "yyyy-MM-dd HH:mm:ss"));
		buffer.append("#r#W");
		buffer.append(bean.getMessage());
		RichLabel richLabel = getRichLabel(rolename);
		richLabel.addText(buffer.toString());
		richLabel.setPreferredSize(richLabel.computeSize(richLabel.getWidth()));
		if (FormsManagement.getframe(56).isVisible()) {
			JScrollPane js = FriendChatMessageJframe.getFriendChatMessageJframe().getJpanel().getJs();
			js.getViewport().setViewSize(richLabel.getPreferredSize());
			js.getVerticalScrollBar().setValue(js.getVerticalScrollBar().getMaximum());
		} else if (!NotReads.contains(bean.getRolename())) {
			NotReads.add(bean.getRolename());
		}

	}
	public static String timeStamp2Date(long seconds, String format) {
		if (format == null || format.isEmpty()) {
			format = "yyyy-MM-dd HH:mm:ss";
		}

		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(new Date(seconds));
	}

	public static void ShowMessage(String rolename) {
		for(int i = 0; i < UserMessUntil.getFriendtables().size(); ++i) {
			Friendtable friend = (Friendtable)UserMessUntil.getFriendtables().get(i);
			if (friend.getRolename().equals(rolename)) {
				FriendChatMessageJframe.getFriendChatMessageJframe().getJpanel().showFriend(friend, getRichLabel(friend.getRolename()));
				FormsManagement.showForm(56);
				return;
			}
		}

		Friendtable friend = new Friendtable();
		friend.setRolename(rolename);
		friend.setRace_name("非好友");
		friend.setGrade(new BigDecimal(0));
		friend.setRole_id(new BigDecimal(0));
		FriendChatMessageJframe.getFriendChatMessageJframe().getJpanel().showFriend(friend, getRichLabel(rolename));
		FormsManagement.showForm(56);
	}

	public static void ReceiveFriend() {
		if (NotReads.size() != 0) {
			ShowMessage((String)NotReads.get(NotReads.size() - 1));
			NotReads.remove(NotReads.size() - 1);
		} else {
			if (!FormsManagement.getframe(4).isVisible()) {
				String mes = Agreement.getAgreement().friendAgreement(ImageMixDeal.userimg.getRoleShow().getRole_id().toString());
				SendMessageUntil.toServer(mes);
				JTreeData.ShowFriendMsg(TestfriendlistJframe.getTestfriendlistJframe().getJflist().getTop(), TestfriendlistJframe.getTestfriendlistJframe().getJflist().getjTree());
				FormsManagement.showForm(4);
				Music.addyinxiao("打开窗口.mp3");
			} else {
				FormsManagement.HideForm(4);
				Music.addyinxiao("关闭窗口.mp3");
			}

		}
	}
}
